<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Category;
use app\common\model\Article;
use app\admin\model\Admin;
use app\common\model\Department;
use app\common\model\Duty;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }
    public function nav()
    {
        $where=[];
        $res=Db::name('nav')->where($where)->order('weigh asc')->select();
        return $this->success('成功',$res);
    }
    public function banner(){
        $where=[];
        $res=Db::name('banner')->where($where)->order('weigh asc')->select();
        return $this->success('成功',$res);
    }
    //活动报名
    public function activity(){
        $where=[];
        $where['is_show']=1;
        $res=Db::name('active')->where($where)->select();
        return $this->success('成功',$res);
    }
    public function activityindex(){
        $ids=input('post.id');
        $where=[];
        $where['is_show']=1;
        $where['id']=$ids;
        $res=Db::name('active')->where($where)->find();
        return $this->success('成功',$res);
    }
    //首页广告位
    public function adnew(){
        $where=[];
        $res=Db::name('ad')->where($where)->order('id desc')->find();
        return $this->success('成功',$res);
    }
    //获取分类列表
    public function catelist(){
        $cat_id=input('param.cate_id');
        if (empty($cat_id)){
            $this->error('信息错误');
        }
        $res=Category::with(['article'])->where(['id'=>$cat_id])->find();
        if ($cat_id==26) {
            //获取所偶的pid
             $res=Category::where(['id'=>$cat_id])->find();
             if (!empty($res)) {
// <<<<<<< Updated upstream
                $subQuery = Db::name('article')
                ->where(['is_show'=>1])
                ->order('createtime desc')
                ->buildSql();
            //然后使用子查询构造新的查询
            $list = Db::table($subQuery.' a')
                ->group('ks_text')
                ->select();

                        foreach ($list as $k2 => $v2) {
                            $list[$k2]=Db::name('article')->where(['is_show'=>1,'ks_text'=>$v2['ks_text']])->order('createtime desc')->find();
                            if (!empty($list[$k2]) && !empty($list[$k2]['title'])) {
                                // code...
                                $list[$k2]['createtime']=date("m/d",$list[$k2]['createtime']);
                                
                            }else{
                                if (!empty($list[$k2]) && empty($list[$k2]['title'])) {
                                    $list[$k2]['createtime']='';
                                }
                            }
                            
                            $v2=$list[$k2];
                    $newtypes=Category::where($v2['category_id'])->value('type');
                    $myname="";
                    switch ($newtypes) {
                        case 'default':
                            $myname="链接";
                            break;
                        case 'article':
                            $myname="文章";
                            break;
                        case 'article':
                            $myname="图片";
                            break;
                        case 'article':
                            $myname="文件下载";
                            break;
                        case 'article':
                            $myname="详情";
                            break;
                        default:
                            // code...
                            break;
                    }
                    $list[$k2]['newtypes']=$myname;
                    $list[$k2]['jumptype']=Category::where('id',$v2['category_id'])->value('type');
                   $list[$k2]['adminname']=Admin::where('id',$v2['admin_id'])->value('username');
                }
                 $res['article']=$list;
       
                //  $res=Category::with(['article1'])->where(['id'=>$cat_id])->find();
                //  $res['article']=$res['article1'];
                 $resnewdata=['办公室','政治工作科','教学训练科','教务科','后勤科','学员管理科'];
// =======
//                  // $res['article']=Article::where(['category_id'=>$cat_id])->where(['is_show'=>1])->group('ks_text')->order('createtime desc')->select();
//                  $res=Category::with(['article1'])->where(['id'=>$cat_id])->find();
//                  $res['article']=$res['article1'];
//                  $resnewdata=['政治工作科','后勤科','办公室','教务科','教学训练科','学员管理科'];
// >>>>>>> Stashed changes
                 $newarr = [];
                 $nedata=$res['article'];
                $nedata=array_column($nedata,null,'ks_text');
                foreach ($resnewdata as $k1 => $v1) {
                    if (array_key_exists($v1,$nedata)) {
                        $newarr[$k1]=$nedata[$v1];
                    }
                }
                $newarr=array_values($newarr);
                $res['article']=$newarr;
             }


      
        }
        return $this->success('成功',$res);

    }
    //获取一级分类下面所有的二级分类
    public function cateall(){
        $catepid=input('post.catepid');
        if (empty($catepid)){
            $this->error('信息错误');
        }
        $rs1=Db::name('category')->where(['id'=>$catepid])->field('id,name')->find();
        $res=Category::where(['pid'=>$catepid])->select();
//        $newdata=[];
//        $newdata[]=$rs1;
        if ($catepid==25) {
            foreach ($res as $k1 => $v1) {
//            $newdata[]=$v1;
                if ($v1['id']=="26") {
                    // code...
                    unset($res[$k1]);
                }
            }
            // code...
        }
        $res=array_values($res);
//
        return json_encode(['code'=>1,'msg'=>'成功','time'=>time(),'data'=>$res,'newdata'=>$rs1]);
        return $this->success('成功',$res);
    }
    //今日值班
    public function duty(){
        $res=Duty::with(['people1','people2','people3','people4'])->whereTime('duty_time', 'd')->find();
            $nestal['people']=[];
        $nestal['people']['0']['name']="带班主官";
        $nestal['people']['0']['value']="";
        $nestal['people']['1']['name']="值班领导";
        $nestal['people']['1']['value']="";
        $nestal['people']['2']['name']="指挥长";
        $nestal['people']['2']['value']="";
        $nestal['people']['3']['name']="战保助理";
        $nestal['people']['3']['value']="";
        if (!empty($res['people1'])){
            $nestal['people']['0']['value']=$res['people1']['name'];
        }
        if (!empty($res['people2'])){
            $nestal['people']['1']['value']=$res['people2']['name'];
        }
        if (!empty($res['people3'])){
            $nestal['people']['2']['value']=$res['people3']['name'];
        }
        if (!empty($res['people4'])){
            $nestal['people']['3']['value']=$res['people4']['name'];
        }
        $newdata=[];
//        $newdata['newdata']=$nestal;
        $resdata=[];
        $resdata[]=['name'=>"山西消防信息网",'url'=>"http://59.195.205.136/"];
        $resdata[]=['name'=>"全国消防机构",'url'=>"http://59.255.109.9/map/map.html"];
        $resdata[]=['name'=>"全省消防导航",'url'=>"https://www.baidu.com",'type'=>"article",'id'=>42];
        $resdata[]=['name'=>"业务系统导航",'url'=>"https://www.baidu.com",'type'=>"article",'id'=>43];
        $newdata['newdata']=$nestal;
        $newdata['nav']=$resdata;
        return $this->success('成功',$newdata);
    }
    public function allteaining(){
        $where=[];
        $res=Db::name('training')->where($where)->order('id desc')->select();
        return $this->success('成功',$res);
    }
    //搜索接口
    public function searchlist()
    {
        $where=[];
        $page=input('post.page',1);
        $size=input('post.size',10);
        $name=input('post.name');
        if(empty($name)){
            $this->error('搜索值不能为空');
        }
        $where['is_show']=1;
        $where['title']=['like','%'.$name.'%'];
        $res=Article::where($where)->paginate($size,false,['page'=>$page]);
        return $this->success('成功',$res);
    }
    //分类

    public function articlelist()
    {
        $where=[];
        $page=input('post.page',1);
        $size=input('post.size',10);
        $cate_id=input('post.cate_id');
        if(empty($cate_id)){
            $this->error('分类不能为空');
        }
        $where['is_show']=1;
        $where['category_id']=$cate_id;
        $res=Article::where($where)->order('createtime desc')->paginate($size,false,['page'=>$page]);
        return $this->success('成功',$res);
    }
    //部门
    public function department(){
       $res= Department::with(['communication'])->select();
             return $this->success('成功',$res);
    }
    //详情
    public function articledeail(){
        $id=input('post.id');
        if(empty($id)){
            $this->error('信息不能为空');
        }
        $res=Article::where(['id'=>$id])->find();
        //获取下一条
        if (!empty($res)){
            $res['next']=Article::where('id','>',$id)->where(['category_id'=>$res['category_id']])->order('id asc')->field('id,title,category_id,admin_id')->find();
        }
        return $this->success('成功',$res);
    }
    //详情
    public function wydetail(){
        $name=input("post.name",'bmnew');
        
        $res=Db::name('config')->where(['name'=>$name])->value('value');
        return $this->success('成功',$res);
    }
}
